import { BaseEnum } from '@/enums/base'

/**
 * 物资订单审批状态枚举
 * 用于标识订单在审批流程中的状态
 */
export const OrderReviewStatus = {
  /**
   * 无状态
   */
  None: {
    code: 'None',
    name: '无状态',
    index: 0,
    description: '初始状态，未开始任何流程',
  },

  /**
   * 出库申请
   */
  OutboundApply: {
    code: 'OutboundApply',
    name: '出库申请',
    index: 1,
    description: '申请物资出库',
  },

  /**
   * 出库审核通过
   */
  OutboundApprove: {
    code: 'OutboundApprove',
    name: '出库审核通过',
    index: 2,
    description: '出库申请已通过审核',
  },

  /**
   * 出库完成
   */
  OutboundComplete: {
    code: 'OutboundComplete',
    name: '出库完成',
    index: 3,
    description: '物资已完成出库',
  },

  /**
   * 入库申请
   */
  InboundApply: {
    code: 'InboundApply',
    name: '入库申请',
    index: 4,
    description: '申请物资入库',
  },

  /**
   * 入库审核通过
   */
  InboundApprove: {
    code: 'InboundApprove',
    name: '入库审核通过',
    index: 5,
    description: '入库申请已通过审核',
  },

  /**
   * 入库完成
   */
  InboundComplete: {
    code: 'InboundComplete',
    name: '入库完成',
    index: 6,
    description: '物资已完成入库',
  },

  /**
   * 流程终止
   */
  Terminate: {
    code: 'Terminate',
    name: '流程终止',
    index: 7,
    description: '订单流程被终止',
  },
} as const

// 待审批的状态

/**
 * 根据code获取枚举
 * @param code
 * @returns 枚举值或null
 */
export function matchCode(code: string) {
  return Object.values(OrderReviewStatus).find((item) => item.code === code) || null
}

/**
 * 根据index获取枚举
 * @param index
 * @returns 枚举值或null
 */
export function matchIndex(index: number) {
  return Object.values(OrderReviewStatus).find((item) => item.index === index) || null
}

/**
 * 获取所有订单审批状态选项
 * @returns 订单审批状态数组
 */
export function getOrderReviewStatusOptions() {
  return Object.values(OrderReviewStatus)
}

/**
 * 判断是否为出库相关状态
 * @param code 状态代码
 * @returns boolean
 */
export function isOutboundStatus(code: string): boolean {
  const outboundCodes: string[] = [
    OrderReviewStatus.OutboundApply.code,
    OrderReviewStatus.OutboundApprove.code,
    OrderReviewStatus.OutboundComplete.code,
  ]
  return outboundCodes.includes(code)
}

/**
 * 判断是否为入库相关状态
 * @param code 状态代码
 * @returns boolean
 */
export function isInboundStatus(code: string): boolean {
  const inboundCodes: string[] = [
    OrderReviewStatus.InboundApply.code,
    OrderReviewStatus.InboundApprove.code,
    OrderReviewStatus.InboundComplete.code,
  ]
  return inboundCodes.includes(code)
}

/**
 * 状态分组定义
 */
export const StatusGroups = {
  /** 待审批状态 */
  PENDING_APPROVAL: {
    code: 'PENDING_APPROVAL',
    name: '待审批',
    statuses: [
      OrderReviewStatus.None.code,
      OrderReviewStatus.OutboundApply.code,
      OrderReviewStatus.InboundApply.code,
    ],
  },
  /** 已审批状态 */
  APPROVED: {
    code: 'APPROVED',
    name: '已审批',
    statuses: [
      OrderReviewStatus.OutboundApprove.code,
      OrderReviewStatus.OutboundComplete.code,
      OrderReviewStatus.InboundApprove.code,
      OrderReviewStatus.InboundComplete.code,
      OrderReviewStatus.Terminate.code,
    ],
  },
} as const

/**
 * 判断是否为待审批状态
 * @param code 状态代码
 * @returns boolean
 */
export function isPendingApprovalStatus(code: string): boolean {
  return (StatusGroups.PENDING_APPROVAL.statuses as readonly string[]).includes(code)
}

/**
 * 判断是否为已审批状态
 * @param code 状态代码
 * @returns boolean
 */
export function isApprovedStatus(code: string): boolean {
  return (StatusGroups.APPROVED.statuses as readonly string[]).includes(code)
}

/**
 * 获取状态所属分组
 * @param code 状态代码
 * @returns 分组信息或null
 */
export function getStatusGroup(code: string) {
  if (isPendingApprovalStatus(code)) {
    return StatusGroups.PENDING_APPROVAL
  }
  if (isApprovedStatus(code)) {
    return StatusGroups.APPROVED
  }
  return null
}

/**
 * 获取状态的颜色类型（用于UI展示）
 * @param status 状态枚举值
 * @returns Element Plus 的 tag 类型
 */
export function getStatusType(
  status: (typeof OrderReviewStatus)[keyof typeof OrderReviewStatus],
): 'info' | 'warning' | 'success' | 'danger' {
  switch (status.code) {
    case OrderReviewStatus.None.code:
      return 'info'
    case OrderReviewStatus.OutboundApply.code:
    case OrderReviewStatus.InboundApply.code:
      return 'warning'
    case OrderReviewStatus.OutboundApprove.code:
    case OrderReviewStatus.InboundApprove.code:
    case OrderReviewStatus.OutboundComplete.code:
    case OrderReviewStatus.InboundComplete.code:
      return 'success'
    case OrderReviewStatus.Terminate.code:
      return 'danger'
    default:
      return 'info'
  }
}
